package com.company.cloud.oss.utils;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.company.cloud.oss.config.OssConfig;
import com.company.cloud.oss.constant.OssTypeEnum;
import com.company.cloud.oss.modules.cloud.CloudStorageService;
import com.company.cloud.oss.modules.cloud.OSSFactory;
import com.company.cloud.oss.modules.cloud.models.ImgInfoModel;

public class OssTestUtils {


    public static String getTargetImage(String sourceName,String resizeName){
        String targetName;
        int idx=sourceName.lastIndexOf(".");
        if(idx==-1){
            targetName=StrUtil.format("{}{}",sourceName,resizeName);
        }else if(idx==(sourceName.length()-1)){
            targetName=StrUtil.format("{}{}.",sourceName.substring(0,idx),resizeName);
        }else{
            String str=sourceName.substring(idx);
            if(StrUtil.isEmpty(str)){
                targetName=StrUtil.format("{}{}",sourceName,resizeName);;
            }else{
                targetName=sourceName.replace(str,resizeName+str);
            }
        }
        return targetName;
    }

    public static void main(String[] args) {

//        OssConfig  config=new OssConfig();
//        config.setOssType(OssTypeEnum.OSS_ALIYUN.getCode());
//        config.setAccessId("LTAIeR5j6Kst1dK1");
//        config.setAccessKey("GLKBLjj8CkOsYsQqbwBBOd51qPGeNc");
//        config.setEndpoint("oss-cn-shanghai.aliyuncs.com");
//        config.setBucket("wang20");
//        CloudStorageService cloudStorageService=OSSFactory.build(config);

//        ImgInfoModel  entity= cloudStorageService.getImgInfo(
//                        "https://wang20.oss-cn-shanghai.aliyuncs.com/8c48d543fe194c8e9c50052758a742c0_1869_100_200.jpg");
//        System.out.println(JSON.toJSONString(entity));
//
//        ImgInfoModel  entity2= cloudStorageService.getImgInfo(
//                "https://wang20.oss-cn-shanghai.aliyuncs.com/8c48d543fe194c8e9c50052758a742c0_1869_100.jpg");
//        System.out.println(JSON.toJSONString(entity2));


//        h:937 w:980
//        "imageHeight":{"value":"382"},"imageWidth":{"value":"400"}
//        imageHeight":{"value":"287"},"imageWidth":{"value":"300"}
//        imageHeight":{"value":"191"},"imageWidth":{"value":"200"}
//        imageHeight":{"value":"96"},"imageWidth":{"value":"100"}



//        int w=980;
//        int h=937;
//
//        int w1=200;
//        float h1=(float)w1/w *h ;
//
//
//        System.out.println(h1);
//        System.out.println(Math.round(h1));


//        String json= cloudStorageService.process2SaveAs("image/resize,w_100,h_200/format,jpg",
//                "8c48d543fe194c8e9c50052758a742c0_1869.jpg",
//                "8c48d543fe194c8e9c50052758a742c0_1869_100_200.jpg");
//        System.out.println(json);


        String s2= OssTestUtils.getTargetImage("8c48d543fe194c8e9c50052758a742c0_1869.jpg","-100-100");
        System.out.println(s2);






    }
}
